﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using HA.Data;

namespace HA.ModFlow.Controls.Windows.MapData
{
    /// <summary>
    /// winCoverageSetup.xaml 的交互逻辑
    /// </summary>
    public partial class winCoverageSetup : Window
    {
        public winCoverageSetup(Coverage coverage)
        {
            InitializeComponent();

            this._coverage = coverage;

            this._coverageSetup = new PCCoverageSetup(coverage);

            this.DataContext = this._coverageSetup;

            this.lstSctypes.ItemsSource = this._coverageSetup.ScTypes;
            this.lstArealTypes.ItemsSource = this._coverageSetup.ArealTypes;

            IsLPF = coverage.ParentCM.Model.InnerPack == Domain.DomainLayer.INNERPACK.LPF;
        }

        Coverage _coverage;

        bool IsLPF;

        ListCollectionView lcv;

        PCCoverageSetup _coverageSetup;

        public PCCoverageSetup PCCoverageSetup
        {
            get { return _coverageSetup; }
            set { _coverageSetup = value; }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {

            this._coverageSetup.Confirm();

            this.DialogResult = true;
            this.Close();


        }

        private void btnCancal_Click(object sender, RoutedEventArgs e)
        {

            this.Close();

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            switch (_coverage.CoverageType)
            {
                case HAUtil.LAYERCOVERAGETYPE.对象层:
                    this.chkBoxDefBounder.Visibility = System.Windows.Visibility.Collapsed;
                    this.grpBoxSourceSink.Visibility = System.Windows.Visibility.Visible;
                    this.grpBoxArea.Visibility = System.Windows.Visibility.Collapsed;
                    this.grpBoxView.Visibility = System.Windows.Visibility.Collapsed;
                    break;
                case HAUtil.LAYERCOVERAGETYPE.边界层:
                    this.grpBoxSourceSink.Visibility = System.Windows.Visibility.Visible;
                    this.grpBoxArea.Visibility = System.Windows.Visibility.Collapsed;
                    this.grpBoxView.Visibility = System.Windows.Visibility.Collapsed;
                    break;
                case HAUtil.LAYERCOVERAGETYPE.蒸散层:
                    this.chkBoxDefBounder.Visibility = System.Windows.Visibility.Collapsed;
                    this.stkPanelElev.Visibility = System.Windows.Visibility.Collapsed;
                    this.stkPanelRange.Visibility = System.Windows.Visibility.Collapsed;
                    this.grpBoxSourceSink.Visibility = System.Windows.Visibility.Collapsed;
                    this.grpBoxArea.Visibility = System.Windows.Visibility.Visible;
                    this.grpBoxView.Visibility = System.Windows.Visibility.Collapsed;
                    this.stkPanelElev.Visibility = System.Windows.Visibility.Collapsed;
                    this.stkPanelRange.Visibility = System.Windows.Visibility.Collapsed;
                    this.chkBoxDefBounder.Visibility = System.Windows.Visibility.Collapsed;
                    break;
                case HAUtil.LAYERCOVERAGETYPE.补给层:
                    this._coverageSetup.Setup.ArealTypes[MAPArealType.RECHARGERATE] = true;
                    if (this._coverage.ParentCM.Model.IsTransPort)
                        this._coverageSetup.Setup.ArealTypes[MAPArealType.RECHARGECONC] = true;
                    this.DialogResult = true;
                    this.Close();
                    break;
                case HAUtil.LAYERCOVERAGETYPE.水文地质层:
                    if (!_coverage.CheckedGeoParam[0])
                    {
                        this.chkBoxDefBounder.Visibility = System.Windows.Visibility.Collapsed;
                        this.stkPanelElev.Visibility = System.Windows.Visibility.Collapsed;
                        this.stkPanelRange.Visibility = System.Windows.Visibility.Collapsed;
                    }
                    this.grpBoxSourceSink.Visibility = System.Windows.Visibility.Collapsed;
                    this.grpBoxArea.Visibility = System.Windows.Visibility.Visible;
                    this.grpBoxView.Visibility = System.Windows.Visibility.Collapsed;
                    break;
                default:
                    break;
            }

            lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(this.lstSctypes.ItemsSource);
            if (lcv != null)
            {
                lcv.Filter = h =>
                    {
                        switch (((PCSCKVItem)h).Key)
                        {
                            case MAPSCType.LAYERRANGE:
                                return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.对象层;
                            case MAPSCType.WELL:
                                return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.对象层;
                            case MAPSCType.REFINEPOINTS:
                                return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.对象层;
                            case MAPSCType.SPECIFIEDHEAD:
                                return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.边界层;
                            case MAPSCType.SPECIFIEDFLOW:
                                return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.边界层;
                            case MAPSCType.GENERALHEAD:
                                return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.对象层;
                            case MAPSCType.DRAIN:
                                return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.对象层;
                            case MAPSCType.RIVER:
                                return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.对象层;
                            case MAPSCType.LAKE:
                                return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.对象层;
                            case MAPSCType.BARRIER:
                                return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.对象层;
                            case MAPSCType.OBSERVEDFLOW:
                                return false;
                            case MAPSCType.TANSOBSERVEDFLOW:
                                return false;
                            case MAPSCType.SEEPAGEFACE:
                                return false;
                            case MAPSCType.NONE:
                                return false;
                            case MAPSCType.SPECIFIEDCONC:
                                return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.对象层;
                            case MAPSCType.MASSLOADING:
                                return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.对象层;
                            default:
                                return false;

                        }
                    };
                lcv.Refresh();
            }

            lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(this.lstArealTypes.ItemsSource);

            if (lcv != null)
            {
                lcv.Filter = h =>
                {
                    switch (((PCArealKVItem)h).Key)
                    {
                        //case MAPArealType.LAYERANGE:
                        //    return true;
                        case MAPArealType.RECHARGERATE:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.补给层;
                        case MAPArealType.HORIZONTALK:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.CheckedGeoParam[1];
                        case MAPArealType.VERTICLEK:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.CheckedGeoParam[1] && IsLPF;
                        case MAPArealType.HORIZONTALANIS:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.CheckedGeoParam[1];
                        case MAPArealType.VERTICLEANIS:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.CheckedGeoParam[1];
                        case MAPArealType.SPECIFICSTORAGE:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.CheckedGeoParam[1] && IsLPF;
                        case MAPArealType.SPECIFICYIELD:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.CheckedGeoParam[1] && IsLPF;
                        case MAPArealType.TOPELVE:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.CheckedGeoParam[0];
                        case MAPArealType.BOTELVE:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.CheckedGeoParam[0];
                        case MAPArealType.WETDRYFALG:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.CheckedGeoParam[1];
                        case MAPArealType.MAXETRATE:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.蒸散层;
                        case MAPArealType.ETELEV:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.蒸散层;
                        case MAPArealType.ETEXTINCTIONDEPTH:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.蒸散层;
                        case MAPArealType.TRANSMISSITY:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.CheckedGeoParam[1] && !IsLPF;
                        case MAPArealType.LEAKANCE:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.CheckedGeoParam[1] && !IsLPF;
                        case MAPArealType.PrimaryStorage:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.CheckedGeoParam[1] && !IsLPF;
                        case MAPArealType.SecondaryStorage:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.CheckedGeoParam[1] && !IsLPF;
                        case MAPArealType.POROSITY:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.ParentCM.Model.IsTransPort;
                        case MAPArealType.LONGDISPERSITY:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.ParentCM.Model.IsTransPort;
                        case MAPArealType.BULKDENSITY:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.ParentCM.Model.IsTransPort;
                        case MAPArealType.IMMOBILEPOROSITY:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.ParentCM.Model.IsTransPort;
                        case MAPArealType.STARTINGCONCENTRATION:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.水文地质层 && _coverage.ParentCM.Model.IsTransPort;
                        case MAPArealType.RECHARGECONC:
                            return _coverage.CoverageType == HAUtil.LAYERCOVERAGETYPE.补给层 && _coverage.ParentCM.Model.IsTransPort;
                        default:
                            return true;
                    }

                    //switch (((PCArealKVItem)h).Key)
                    //{
                    //    case MAPArealType.VERTICLEK:
                    //        return IsLPF;
                    //    case MAPArealType.SPECIFICSTORAGE:
                    //        return IsLPF;
                    //    case MAPArealType.SPECIFICYIELD:
                    //        return IsLPF;
                    //    case MAPArealType.TRANSMISSITY:
                    //        return !IsLPF;
                    //    case MAPArealType.LEAKANCE:
                    //        return !IsLPF;
                    //    case MAPArealType.PrimaryStorage:
                    //        return !IsLPF;
                    //    case MAPArealType.SecondaryStorage:
                    //        return !IsLPF;
                    //    default:
                    //        return true;
                    //}

                };

                lcv.Refresh();
            }
        }


    }

    class MAPSCTypeConvertToCN : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            MAPSCType typeValue = (MAPSCType)value;

            switch (typeValue)
            {
                case MAPSCType.LAYERRANGE:
                    return "层范围";
                case MAPSCType.WELL:
                    return "井";
                case MAPSCType.REFINEPOINTS:
                    return "参照点";
                case MAPSCType.SPECIFIEDHEAD:
                    return "定水头";
                case MAPSCType.SPECIFIEDFLOW:
                    return "给定流量";
                case MAPSCType.GENERALHEAD:
                    return "通用水头";
                case MAPSCType.DRAIN:
                    return "排水渠";
                case MAPSCType.RIVER:
                    return "河流";
                case MAPSCType.LAKE:
                    return "湖泊";
                case MAPSCType.BARRIER:
                    return "屏障";
                case MAPSCType.OBSERVEDFLOW:
                    return "OBSERVEDFLOW";
                case MAPSCType.TANSOBSERVEDFLOW:
                    return "TANSOBSERVEDFLOW";
                case MAPSCType.SEEPAGEFACE:
                    return "SEEPAGEFACE";
                case MAPSCType.NONE:
                    return "NONE";
                case MAPSCType.SPECIFIEDCONC:
                    return "给定浓度";
                case MAPSCType.MASSLOADING:
                    return "质量负载";
                default:
                    throw new InvalidCastException();
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string strValue = (string)value;
            switch (strValue)
            {
                case "层范围":
                    return MAPSCType.LAYERRANGE;
                case "井":
                    return MAPSCType.WELL;
                case "参照点":
                    return MAPSCType.REFINEPOINTS;
                case "定水头":
                    return MAPSCType.SPECIFIEDHEAD;
                case "给定流量":
                    return MAPSCType.SPECIFIEDFLOW;
                case "通用水头":
                    return MAPSCType.GENERALHEAD;
                case "排水渠":
                    return MAPSCType.DRAIN;
                case "河流":
                    return MAPSCType.RIVER;
                case "湖泊":
                    return MAPSCType.LAKE;
                case "屏障":
                    return MAPSCType.BARRIER;
                case "OBSERVEDFLOW":
                    return MAPSCType.OBSERVEDFLOW;
                case "TANSOBSERVEDFLOW":
                    return MAPSCType.TANSOBSERVEDFLOW;
                case "SEEPAGEFACE":
                    return MAPSCType.SEEPAGEFACE;
                case "NONE":
                    return MAPSCType.NONE;
                case "给定浓度":
                    return MAPSCType.SPECIFIEDCONC;
                case "质量负载":
                    return MAPSCType.MASSLOADING;
                default:
                    throw new InvalidCastException();

            }
        }
    }

    class MAPArealTypeConvertToCN : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            MAPArealType typeValue = (MAPArealType)value;

            switch (typeValue)
            {
                case MAPArealType.LAYERANGE:
                    return "层范围";
                case MAPArealType.RECHARGERATE:
                    return "补给通量";
                case MAPArealType.RECHARGECONC:
                    return "补给浓度";
                case MAPArealType.HORIZONTALK:
                    return "水平渗透系数";
                case MAPArealType.VERTICLEK:
                    return "垂向渗透系数";
                case MAPArealType.HORIZONTALANIS:
                    return "水平异质性";
                case MAPArealType.VERTICLEANIS:
                    return "垂向异质性";
                case MAPArealType.SPECIFICSTORAGE:
                    return "贮水系数";
                case MAPArealType.SPECIFICYIELD:
                    return "给水度";
                case MAPArealType.TOPELVE:
                    return "顶层海拔";
                case MAPArealType.BOTELVE:
                    return "底部海拔";
                case MAPArealType.WETDRYFALG:
                    return "干湿转换标志";
                case MAPArealType.MAXETRATE:
                    return "最大蒸散通量";
                case MAPArealType.ETELEV:
                    return "蒸散界面海拔";
                case MAPArealType.ETEXTINCTIONDEPTH:
                    return "蒸散终止深度";
                case MAPArealType.TRANSMISSITY:
                    return "导水系数";
                case MAPArealType.LEAKANCE:
                    return "垂向水力传导率";
                case MAPArealType.PrimaryStorage:
                    return "第一类贮水系数";
                case MAPArealType.SecondaryStorage:
                    return "第二类贮水系数";
                case MAPArealType.POROSITY:
                    return "有效孔隙度";
                case MAPArealType.LONGDISPERSITY:
                    return "有效分子扩散系数";
                case MAPArealType.BULKDENSITY:
                    return "含水层介质的容重";
                case MAPArealType.IMMOBILEPOROSITY:
                    return "不流动区域的孔隙度";
                case MAPArealType.STARTINGCONCENTRATION:
                    return "初始浓度";
                default:
                    throw new InvalidCastException();
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string cnValue = (string)value;

            switch (cnValue)
            {
                case "层范围":
                    return MAPArealType.LAYERANGE;
                case "补给通量":
                    return MAPArealType.RECHARGERATE;
                case "补给浓度":
                    return MAPArealType.RECHARGECONC;
                case "水平渗透系数":
                    return MAPArealType.HORIZONTALK;
                case "垂向渗透系数":
                    return MAPArealType.VERTICLEK;
                case "水平异质性":
                    return MAPArealType.HORIZONTALANIS;
                case "垂向异质性":
                    return MAPArealType.VERTICLEANIS;
                case "贮水系数":
                    return MAPArealType.SPECIFICSTORAGE;
                case "给水度":
                    return MAPArealType.SPECIFICYIELD;
                case "顶层海拔":
                    return MAPArealType.TOPELVE;
                case "底部海拔":
                    return MAPArealType.BOTELVE;
                case "干湿转换标志":
                    return MAPArealType.WETDRYFALG;
                case "最大蒸散通量":
                    return MAPArealType.MAXETRATE;
                case "蒸散界面海拔":
                    return MAPArealType.ETELEV;
                case "蒸散终止深度":
                    return MAPArealType.ETEXTINCTIONDEPTH;
                case "导水系数":
                    return MAPArealType.TRANSMISSITY;
                case "垂向水力传导率":
                    return MAPArealType.LEAKANCE;
                case "第一类贮水系数":
                    return MAPArealType.PrimaryStorage;
                case "第二类贮水系数":
                    return MAPArealType.SecondaryStorage;
                case "有效孔隙度":
                    return MAPArealType.POROSITY;
                case "有效分子扩散系数":
                    return MAPArealType.LONGDISPERSITY;
                case "含水层介质的容重":
                    return MAPArealType.BULKDENSITY;
                case "不流动区域的孔隙度":
                    return MAPArealType.IMMOBILEPOROSITY;
                case "初始浓度":
                    return MAPArealType.STARTINGCONCENTRATION;
                default:
                    throw new InvalidCastException();
            }
        }
    }

    class MAPObservationTypeConvertToCN : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            MAPObservationType typeValue = (MAPObservationType)value;

            switch (typeValue)
            {
                case MAPObservationType.HEAD:
                    return "水头";
                default:
                    throw new InvalidCastException();
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string cnValue = (string)value;

            switch (cnValue)
            {
                case "水头":

                    return MAPObservationType.HEAD;
                default:
                    throw new InvalidCastException();
            }
        }
    }

}
